cd D:\Dropbox\book_welfare\replication 

************************************************
* coefficient by reader type: Goodreads 
* femaleness, whether above median for romance reading, etc
************************************************	


*********************
* in 
*********************
use  data\books_year_types.dta, clear  


		keep if year>=2007 & year<=2016
		keep if pubyr >=1960 & pubyr<=2016 
		replace fbookp=2 if fbookp==.
		collapse (count) n=q , by(pubyr fbookp genre1 )
		tempfile gr 
		save `gr'



use   data\books_year_types.dta, clear  


		keep if year>=2007 & year<=2016
		keep if pubyr >=1960 & pubyr<=2016 
	
		replace fbookp=2 if fbookp==.

		collapse (sum) q*, by(year pubyr fbookp genre1 )

		merge m:1 pubyr fbookp genre1 using `gr'

		egen gno=group(genre1)
		
		
		egen Qv_all = sum(q), by(genre1 pubyr year)
		gen sv_all=q/Qv_all
		
		egen Nv_all = sum(n), by(genre1 pubyr year)
		gen rv_all = n/Nv_all

		egen Qv_in = sum(q) if fbookp<2, by(genre1 pubyr year)
		gen sv_in=q/Qv_in
		
		egen Nv_in = sum(n) if fbookp<2, by(genre1 pubyr year)
		gen rv_in = n/Nv_in
		
		
		
		egen QFv = sum(qf), by(genre1 pubyr year)
		gen sfv=qf/QFv
		egen QMv = sum(qm), by(genre1 pubyr year)
		gen smv=qm/QMv

		forvalues k=1(1)10{
			egen Q`k'v = sum(qc`k'), by(genre1 pubyr year)
			gen s`k'v = qc`k'/Q`k'v
		}
	
	
		egen N=sum(n), by(pubyr) 

		label var sv_in "female-authored share of consumption (in)"
		label var rv_in "female-authored share of new products (in)"
			
		label var sv_all "female-authored share of consumption (all)"
		label var rv_all  "female-authored share of new products (all)"
		label var sfv "fem-auth shr of female cons"
		label var smv "fem-auth shr of male cons"

		 
		 

		preserve 
			collapse (mean) gno, by(genre) 
			tempfile genre 
			save `genre'
		restore 
		 
		 gen cc=0
		 gen se=0 
		 forvalues k=1(1)10{
			reghdfe s`k'v rv_in if fbookp==1 [weight=Q`k'v], absorb(gno year pubyr)  
				replace cc=_b[rv_in] if _n==`k'
				replace se=_se[rv_in] if _n==`k'
				
		 }
		 gen count=_n 
		 gen max95 = cc + 1.96*se 
		 gen min95 = cc- 1.96*se 
 
		twoway (scatter cc count) (rcap max95 min95 count) if count<=10 , scheme(lean2) xtitle(male-leaning reader) legend(off)
		graph export "latex_text\figures\coeff_by_reader_type_in.pdf", as(pdf) name("Graph") replace


		gen qrom = qdromance
		* rename qdromance qrom
		gen qnor = q - qrom 

		egen QFNOR = sum(qnor), by(genre1 pubyr year)
		gen sfnor=qnor/QFNOR
		egen QFROM = sum(qrom), by(genre1 pubyr year)
		gen sfrom=qrom/QFROM 

 	reghdfe sfnor rv_in if fbookp==1 , absorb(gno year pubyr)  
		gen cr = _b[rv_in] if _n==1 
		gen ser = _se[rv_in] if _n==1
 	reghdfe sfrom rv_in if fbookp==1 , absorb(gno year pubyr)  
		replace cr = _b[rv_in] if _n==2 
		replace ser = _se[rv_in] if _n==2
		
		 replace max95 = cr + 1.96*ser 
		 replace min95 = cr- 1.96*ser 
		
 twoway (scatter cr count) (rcap max95 min95 count) if cr~=. , scheme(lean2) xlabel( 1	"non-romance reader" 2 "romance reader", labsize(small) angle(forty_five) noticks) yline(1) ytitle(female author share coef) legend(off)  xtitle("")


 * loop through reader types according to genre 
 
 preserve 
	 clear 
	 set obs 1
	 gen var1 = .
	 save "data\type_coef_in.dta", replace
 restore 
 
 foreach x in dromance dchildren dcomics dfantasy dfiction dhistory dmystery dnonfiction dpoetry dyoungadult {
   	preserve 
	gen q`x'_non = q - q`x'
	
	egen QF`x'_non = sum(q`x'_non), by(genre1 pubyr year)
	gen sf`x'_non=q`x'_non/QF`x'_non
	egen QF`x' = sum(q`x'), by(genre1 pubyr year)
	gen sf`x'=q`x'/QF`x' 

 	reghdfe sf`x'_non rv_in if fbookp==1 [weight= QF`x'_non], absorb(gno year pubyr)  
		gen b_non = _b[rv_in] 
		gen se_non = _se[rv_in] 
 	reghdfe sf`x' rv_in if fbookp==1 [weight= QF`x'], absorb(gno year pubyr)  
		gen b_yes = _b[rv_in]  
		gen se_yes = _se[rv_in] 
		
		gen type ="`x'"
		collapse (mean) b* se* , by(type)
		append using "data\type_coef_in.dta"
		save "data\type_coef_in.dta", replace 
		restore  
}


 use data\type_coef_in.dta, clear 
	 duplicates drop 
	 gen dif  = b_yes - b_non 
	 gsort -dif 
	 gen count=_n 
	 
	  gen max_yes95=b_yes+1.96*se_yes 
	  gen min_yes95=b_yes-1.96*se_yes 
	  gen max_non95=b_non+1.96*se_non
	  gen min_non95=b_non-1.96*se_non 

	twoway (scatter b_yes count)  (rcap max_yes95 min_yes95 count )  (scatter b_non count, msymbol(diamond)) (rcap max_non95 min_non95 count ) if b_yes~=. , scheme(lean2) legend(order(1 "heavy users" 3 "light")) xlabel(1"romance" ///
	2	"fantasy" ///
	3	"mystery" ///
	4	"comics" ///
	5	"young adult" ///
	6	"children" ///
	7	"history" ///
	8	"fiction" ///
	9	"poetry" ///
	10	"nonfiction" , labsize(small) angle(forty_five) noticks) xtitle("") yline(1)
   graph export "latex_text\figures\coeff_by_reader_type_genre_in_weighted.pdf", as(pdf) name("Graph") replace

*********************
* all 
*********************
use  data\books_year_types.dta, clear  


		keep if year>=2007 & year<=2016
		keep if pubyr >=1960 & pubyr<=2016 
		replace fbookp=0 if fbookp==.
		collapse (count) n=q , by(pubyr fbookp genre1 )
		tempfile gr 
		save `gr'



use  data\books_year_types.dta, clear  


		keep if year>=2007 & year<=2016
		keep if pubyr >=1960 & pubyr<=2016 
	
		replace fbookp=0 if fbookp==.

		collapse (sum) q*, by(year pubyr fbookp genre1 )

		merge m:1 pubyr fbookp genre1 using `gr'

		egen gno=group(genre1)
		
		
		egen Qv_all = sum(q), by(genre1 pubyr year)
		gen sv_all=q/Qv_all
		
		egen Nv_all = sum(n), by(genre1 pubyr year)
		gen rv_all = n/Nv_all

		egen Qv_in = sum(q) if fbookp<2, by(genre1 pubyr year)
		gen sv_in=q/Qv_in
		
		egen Nv_in = sum(n) if fbookp<2, by(genre1 pubyr year)
		gen rv_in = n/Nv_in
	
	
	
		egen QFv = sum(qf), by(genre1 pubyr year)
		gen sfv=qf/QFv
		egen QMv = sum(qm), by(genre1 pubyr year)
		gen smv=qm/QMv

		forvalues k=1(1)10{
			egen Q`k'v = sum(qc`k'), by(genre1 pubyr year)
			gen s`k'v = qc`k'/Q`k'v
		}
		

	
		egen N=sum(n), by(pubyr) 

		label var sv_in "female-authored share of consumption (in)"
		label var rv_in "female-authored share of new products (in)"
			
		label var sv_all "female-authored share of consumption (all)"
		label var rv_all  "female-authored share of new products (all)"
		label var sfv "fem-auth shr of female cons"
		label var smv "fem-auth shr of male cons"

 
 

		preserve 
			collapse (mean) gno, by(genre) 
			tempfile genre 
			save `genre'
		restore 
		 
		 gen cc=0
		 gen se=0 
		 forvalues k=1(1)10{
			reghdfe s`k'v rv_in if fbookp==1 [weight=Q`k'v], absorb(gno year pubyr)  
				replace cc=_b[rv_in] if _n==`k'
				replace se=_se[rv_in] if _n==`k'
				
		 }
	 gen count=_n 
	 gen max95 = cc + 1.96*se 
	 gen min95 = cc- 1.96*se 
	 
	 twoway (scatter cc count) (rcap max95 min95 count)  (rcap max95 min95 count) if count<=10 , scheme(lean2) xtitle(male-leaning reader) legend(off) yline(1)
	  graph export "latex_text\figures\coeff_by_reader_type_all_weighted.pdf", as(pdf) name("Graph") replace

 

 
 preserve 
	 clear 
	 set obs 1
	 gen var1 = .
	 save "data\type_coef_all.dta", replace
 restore 
 
 foreach x in dromance dchildren dcomics dfantasy dfiction dhistory dmystery dnonfiction dpoetry dyoungadult {
   	preserve 
	gen q`x'_non = q - q`x'
	
	egen QF`x'_non = sum(q`x'_non), by(genre1 pubyr year)
	gen sf`x'_non=q`x'_non/QF`x'_non
	egen QF`x' = sum(q`x'), by(genre1 pubyr year)
	gen sf`x'=q`x'/QF`x' 

 	reghdfe sf`x'_non rv_in if fbookp==1 [weight=QF`x'_non] , absorb(gno year pubyr)  
		gen b_non = _b[rv_in] 
		gen se_non = _se[rv_in] 
 	reghdfe sf`x' rv_in if fbookp==1 [weight=QF`x'], absorb(gno year pubyr)  
		gen b_yes = _b[rv_in]  
		gen se_yes = _se[rv_in] 
		
		gen type ="`x'"
		collapse (mean) b* se* , by(type)
		append using "data\type_coef_all.dta"
		save "data\type_coef_all.dta", replace 
		restore  
}


 use data\type_coef_all.dta, clear 
	 duplicates drop 
	 gen dif  = b_yes - b_non 
	 gsort -dif 
	 gen count=_n 
	 
	  gen max_yes95=b_yes+1.96*se_yes 
	  gen min_yes95=b_yes-1.96*se_yes 
	  gen max_non95=b_non+1.96*se_non
	  gen min_non95=b_non-1.96*se_non 

	twoway (scatter b_yes count)  (rcap max_yes95 min_yes95 count )  (scatter b_non count, msymbol(diamond)) (rcap max_non95 min_non95 count ) if b_yes~=. , scheme(lean2) legend(order(1 "heavy users" 3 "light")) xlabel(1"romance" ///
	2	"fantasy" ///
	3	"mystery" ///
	4	"comics" ///
	5	"young adult" ///
	6	"children" ///
	7	"history" ///
	8	"fiction" ///
	9	"poetry" ///
	10	"nonfiction" , labsize(small) angle(forty_five) noticks) xtitle("") yline(1)
   graph export "latex_text\figures\coeff_by_reader_type_genre_all_weighted.pdf", as(pdf) name("Graph") replace

